﻿using Coolzon.UI.Core.Parts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Coolzon.UI.Core.Runtime.Script.Animation
{
    public class FlyAnimationScriptObject : AnimationScriptObject
    {
        private double _offsetX;
        private double _offsetY;

        public FlyAnimationScriptObject(double offsetX, double offsetY, double duration)
            : base(duration)
        {
            _offsetX = offsetX;
            _offsetY = offsetY;
        }

        public override void AppendToStoryboard(Widget instance, ref double duration, Storyboard storyboard)
        {
            PropertyValueKeeper.Instance.Save(instance, Widget.XLeftProperty, instance.Left);
            PropertyValueKeeper.Instance.Save(instance, Widget.XTopProperty, instance.Top);

            DoubleAnimation animationX = new DoubleAnimation();
            Storyboard.SetTarget(animationX, instance);
            Storyboard.SetTargetProperty(animationX, new System.Windows.PropertyPath(Widget.XLeftProperty));
            animationX.BeginTime = TimeSpan.FromSeconds(duration);
            animationX.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(Duration));
            animationX.From = instance.Left;
            animationX.To = animationX.From.Value + _offsetX;
            storyboard.Children.Add(animationX);

            DoubleAnimation animationY = new DoubleAnimation();
            Storyboard.SetTarget(animationY, instance);
            Storyboard.SetTargetProperty(animationY, new System.Windows.PropertyPath(Widget.XTopProperty));
            animationY.BeginTime = TimeSpan.FromSeconds(duration);
            animationY.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(Duration));
            animationY.From = instance.Top;
            animationY.To = animationY.From.Value + _offsetY;
            storyboard.Children.Add(animationY);

            duration += Duration;
        }
    }
}
